import { RtcDefaultApi } from '@/unit/axios';
import { defaultErrorHandler } from '@/unit/error-handler';
import { success } from '@/unit/message';
import {
  action,
  computed,
  makeObservable,
  observable,
} from 'mobx';
import { singleton } from 'tsyringe';
import { LiveType } from '../home/types';
import { Movie, RoomLiveInfo } from './types';

@singleton()
export class Room {
  @observable public isLoading: boolean = false;

  @observable public roomId: string = '';

  @observable public live: LiveType = new LiveType();

  @observable public movies: Movie[] = [];

  @observable public url: string = '';

  @observable public playUrl: string =
    'https://d2zihajmogu5jn.cloudfront.net/bipbop-advanced/bipbop_16x9_variant.m3u8';

  @observable public roomliveinfo: RoomLiveInfo =
    new RoomLiveInfo();

  /**
   * 远端房间
   */
  @observable public onlineliveinfo: RoomLiveInfo =
    new RoomLiveInfo();

  @action public setLive = (value: LiveType) => {
    this.live = value;
  };

  /**
   * 房间初始化
   * @param roomid
   */
  @action public init = async (roomid: string) => {
    try {
      this.isLoading = true;
      this.roomId = roomid;
      const result = await RtcDefaultApi.apiLiveLivePost({
        id: roomid,
      });
      if (result) {
        this.live = result;
      }
    } catch (error) {
      defaultErrorHandler(error);
    } finally {
      this.isLoading = false;
    }
  };

  /**
   * 获取电影列表 房主
   * @returns
   */
  @action public getMovies = async () => {
    try {
      if (!this.live.id) return;
      const result = await RtcDefaultApi.apiMoviesAllPost({
        liveId: this.live.id,
      });

      if (result) {
        // console.log(result);
        this.movies = result;
        // this.setUrl(this.movies[0].url);
      }
    } catch (error) {
      defaultErrorHandler(error);
    }
  };

  /**
   * 设置地址
   */
  @action public setUrl = (url: string) => {
    this.url = url;
  };

  /**
   * 添加播放地址
   * @returns
   */
  @action public createMovie = async () => {
    try {
      if (!this.live.id) return;
      await RtcDefaultApi.apiMoviesCreatePost({
        url: this.url,
        liveId: this.live.id,
      });

      success('添加成功！');
      this.url = '';
    } catch (error) {
      defaultErrorHandler(error);
    }
  };

  /**
   * 当前播放地址
   * @param value
   */
  @action public setPlay = (value: string) => {
    this.playUrl = value;
    this.roomliveinfo.url = value;
  };

  @action public setDuration = (value: string) => {
    this.roomliveinfo.duration = value ?? '0';
  };

  @action public setOnlineDuration = (value: string) => {
    this.onlineliveinfo.duration = value ?? '0';
  };

  /**
   * 删除视频
   * @param movieId
   */
  @action public deleteMovie = async (movieId: string) => {
    try {
      // const result = await RtcDefaultApi.apiMoviesIdDelete(
      //   {
      //     id: movieId,
      //   },
      //   ''
      // );
      const result =
        await RtcDefaultApi.apiMoviesDeletePost({
          id: movieId,
        });
      if (result) {
        console.log(result);
      }
    } catch (error) {
      defaultErrorHandler(error);
    }
  };

  @computed get moviesList(): Movie[] {
    return this.movies;
  }

  constructor() {
    makeObservable(this);
  }
}
